package com.groupware.message.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.ListModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.RedirectView;

import com.groupware.message.DaoImpl.MessageDaoImpl;
import com.groupware.message.Vo.MessageVo;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 * Handles requests for the application home page.
 */
@Controller
public class MessageController {
	
	private static final Logger logger = LoggerFactory.getLogger(MessageController.class);
	
	@Autowired
	private MessageDaoImpl messageDaoImpl;
	
	@RequestMapping(value = "/sendForm.do", method = RequestMethod.GET)
	public String sendForm(Model model, HttpSession session) {
		String userId = (String)session.getAttribute("userId");
		String userName =messageDaoImpl.getSender(userId);
		List<MessageVo> list = messageDaoImpl.getList();
		model.addAttribute("userName", userName);
		model.addAttribute("list", list);
		System.out.println(list);
		return "message/sendForm";
	}
	
	@RequestMapping(value = "/send.do", method = RequestMethod.POST)
	public RedirectView insertpro(Model model, MessageVo vo) {
		messageDaoImpl.insertMessage(vo);
		return new RedirectView("message.do") ;
	}
	
	@RequestMapping(value = "/receiveForm.do", method = RequestMethod.GET)
	public String receiveForm(Model model, HttpSession session) {
		String userId = (String)session.getAttribute("userId");
		String userName =messageDaoImpl.getSender(userId);
		MessageVo vo= messageDaoImpl.getMessage(userName);
		model.addAttribute("vo",vo);
		System.out.println(userId+userName+vo);
		return "message/receiveForm";
	}
	
	@RequestMapping(value = "/message.do", method = RequestMethod.GET)
	public String messageForm(Model model, HttpSession session) {
		String userId = (String)session.getAttribute("userId");
		String userName =messageDaoImpl.getSender(userId);
		List<MessageVo> list = messageDaoImpl.getList();
		List<MessageVo> recmail = messageDaoImpl.getReceiveMail(userName);
		List<MessageVo> sendmail = messageDaoImpl.getSendMail(userName);
		model.addAttribute("userName", userName);
		model.addAttribute("list", list);
		model.addAttribute("recmail", recmail);
		model.addAttribute("sendmail", sendmail);
		System.out.println(list);
		return "message/message";
	}
	
	@RequestMapping(value = "/sendMailBox.do", method = RequestMethod.GET) //보낸편지함 페이징
	public String sendMailBox(Model model, HttpSession session, HttpServletRequest request) 
	throws ParseException{
		String userId = (String)session.getAttribute("userId");
		String userName =messageDaoImpl.getSender(userId);
		
		int page = 0;
		if(request.getParameter("page") == null){
			page= 1;
		} else{
			page = new Integer(request.getParameter("page"));
		}
		
		int limit =10;
		int start = (page - 1)* limit +1;
		int end = start + limit -1;
		int Count = messageDaoImpl.getCount();
		
		int pageCount=0;
		
		if(Count >0 ){
			pageCount =(int) Count /limit;
			if(Count % limit != 0)
				pageCount++;
		}
		int startpage = (int) ((page-1)/10)* 10+1;
		int endpage = startpage + 10-1;
		if(endpage >= pageCount	)
			endpage = pageCount;
		
		List<MessageVo> SendMB = messageDaoImpl.getPage(start, end);
		
//		List<MessageVo> sendMB = messageDaoImpl.getPage(start, end);
		List<MessageVo> list = messageDaoImpl.getList();
		model.addAttribute("userName", userName);
		model.addAttribute("list", list);
		model.addAttribute("current", page);
		model.addAttribute("start", startpage);
		model.addAttribute("end", endpage);
		model.addAttribute("list", SendMB);
		System.out.println(list);
		return "message/sendMailBox";
	}
	
	@RequestMapping(value = "/receiveMailBox.do", method = RequestMethod.GET) //받은편지함 페이징
	public String receiveMailBox(Model model, HttpSession session) {
		String userId = (String)session.getAttribute("userId");
		String userName =messageDaoImpl.getSender(userId);
		List<MessageVo> list = messageDaoImpl.getList();
		model.addAttribute("userName", userName);
		model.addAttribute("list", list);
		System.out.println(list);
		return "message/receiveMailBox";
	}
	
	
	
}
